package com.lipiao.makerandroid.View.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditActivity extends AppCompatActivity {

    TextView editTextContent_edt;
    EditText editTextContent_edt2;
    EditText editTextContent_edt3;
    EditText editTextContent_edt4;
    EditText editTextContent_edt5;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_about);

        editTextContent_edt = findViewById(R.id.content_edt);
        editTextContent_edt2 = findViewById(R.id.content_edt2);
        editTextContent_edt3 = findViewById(R.id.content_edt3);
        editTextContent_edt4 = findViewById(R.id.content_edt4);
        editTextContent_edt5 = findViewById(R.id.content_edt5);
        Intent i = getIntent();
        String userNumber = i.getStringExtra("userNumber");
        String userName = i.getStringExtra("userName");
        String userEmail = i.getStringExtra("userEmail");
        String userTel = i.getStringExtra("userTel");
        String userGender = i.getStringExtra("userGender");
//        System.out.println(userNumber+userName+userEmail+userTel);
        editTextContent_edt.setText(userNumber);
        if(userName == null){
            editTextContent_edt2.setHint("请填写完整！");
        }else{
            editTextContent_edt2.setText(userName);
        }
        if(userEmail == null){
            editTextContent_edt3.setHint("请填写完整！");
        }else{
            editTextContent_edt3.setText(userEmail);
        }
        if(userTel == null){
            editTextContent_edt4.setHint("请填写完整！");
        }else{
            editTextContent_edt4.setText(userTel);
        }
        if(userGender == null){
            editTextContent_edt5.setHint("请填写性别！");
        }else{
            editTextContent_edt5.setText(userGender);
        }
    }
    public void saveInfo(View view){
        String userNumber = (String) editTextContent_edt.getText();
        String userName = editTextContent_edt2.getText().toString();
        String userEmail = editTextContent_edt3.getText().toString();
        String userTel = editTextContent_edt4.getText().toString();
        String userGender = editTextContent_edt5.getText().toString();
//        System.out.println(userBirth="================");
//        System.out.println(userName!=null&&userEmail!=null&&userTel!=null);
        System.out.println(userNumber+userName+userEmail+userTel+userGender);
        if(userName.equals("")|userEmail.equals("")|userTel.equals("")|userGender.equals("")){
            Toast.makeText(getBaseContext(),"请填写完整！",Toast.LENGTH_SHORT).show();
        }else{
            Call<ResponseBody> callCollectionList = HttpUtil.getUserService().updateUserinfo(userNumber,userName,userEmail,userTel,userGender);
            callCollectionList.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        String strBack = response.body().string();
                        if(strBack.equals("upDateSuccess")){
                            Toast.makeText(getBaseContext(),"更改成功！",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getBaseContext(),"请填写完整！",Toast.LENGTH_SHORT).show();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
        }
    }

    public void back(View view) {
        onBackPressed();
    }

}
